import { IFeidaoAiBrowserComponent } from '@dfeidao/atom-web/interfaces';
import get from '@dfeidao/atom-web/local/get';
import remove from '@dfeidao/atom-web/local/remove';
import javadsd from '@dfeidao/atom-web/msg/javadsd';
import nodejs from '@dfeidao/atom-web/msg/nodejs';
// import whoami from '@dfeidao/atom-web/msg/whoami';
import error from '@dfeidao/atom-web/msgbox/error';
import render from '@dfeidao/atom-web/render/render';

// import render from '@dfeidao/atom-web/render/render';
import a001 from './a001';
import P01 from './P01';

// import P02 from './P02';
import P03 from './P03';
import P05 from './P05';
interface B {
	docDataStore: {
		master: {
			dataArray: [{
				no: string
				formid: string;
				grid_1: {
					dataArray: [
						{
							no: string;
							formid: string;
							formname: string;
							action_name: string;
							action_keyword: string;
						}
					]
				}
			}]
		};
	};
}
export default async function a002(fd: IFeidaoAiBrowserComponent) {
	// const userinf = await whoami();
	// const productid1 = userinf.productid;
	const productid1 = get(fd, 'productid') as string;

	let page_no = Number(get(fd, 'page_no'));
	if (!page_no) {
		page_no = 1;
	}
	let page_size = Number(get(fd, 'page_size'));
	if (!page_size) {
		page_size = 10;
	}
	const name = fd.data.node.querySelector('[data-feidao-id="name"]') as HTMLInputElement;
	const res1 = await nodejs<{ dataArray1: Array<{}>, page_no: number, page_size: number }>('tenant-function/zj-000001/s002', { filter: { tenant_name: name.value, productid: productid1 }, page_no, page_size });// 用户列表初始化查询
	if (res1.dataArray1.length !== 0) {
		res1.page_no = page_no;
		res1.page_size = page_size;
		render(fd, res1, P03, "P03", "inner");
		render(fd, res1.dataArray1, P01, "P01", "inner");
		/////////////////////////// 右侧树//////////////////////////
		const client_type1 = get(fd, 'client_type');
		const condition = {
			param: {
				productid: productid1,
				client_type: client_type1
			}
		};
		const ret = await javadsd('12', '12', 'smart_securitydsd0000001', condition) as B;
		const array = new Array();
		ret.docDataStore.master.dataArray.forEach((i, idex) => {
			if (i.grid_1.dataArray.length > 0) {
				i.grid_1.dataArray.forEach((j) => {
					j.no = j.formid;
					j.formid = j.formid + j.action_keyword;
					j.formname = j.action_name;
					array.push(j);
				});
			}
			delete i.grid_1;
			i.no = '';
			if (!i.formid) {
				i.formid = JSON.stringify(idex);
			}
			array.push(i);
		});
		render(fd, JSON.stringify(array), P05, 'P05', 'inner');
		/////////////////////////// 右侧树//////////////////////////
	} else {
		await a001(fd);
		await error('没有查到对应的租户');
	}
	remove(fd, 'page_size');
	remove(fd, 'page_no');
	remove(fd, 'name');
	remove(fd, 'tenantid');
	remove(fd, 'tenant_level');
}
